<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/10/18
 * Time: 10:03
 */

namespace app\admin\controller;


use app\common\model\CompanyModel;

class CompanyController  extends Base
{
    private $model;

    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->model = new CompanyModel();
    }
    public function index()
    {
        $pageSize = $this->request->param('pageSize/d', 20); //每页多少条
        $pageNumber = $this->request->param('pageNumber/d',1); //当前页


        $list = $this->model
            ->paginate($pageSize, false, ['page' => $pageNumber]);
        $this->result($list, '操作成功', 1);
    }
    public function add()
    {
        $data = $this->request->param();
        $this->validate($data,[
            'name|公司名称'=>'require',

        ]);
        $info = $this->model->where('name',$data['name'])->find();
        if($info) exception('公司名称已存在');
        $result = $this->model->insert($data);
        if($result !== false){
            $this->result($result, '操作成功', 1);
        }
        $this->result($result, '操作失败', 0);
    }
    public function edit(){
        $data = $this->request->param();
        $this->validate($data,[
            'id|操作id'=>'require',
            'name|公司名称'=>'require',

        ]);
        $info = $this->model->where('name',$data['name'])->find();
        if($info && $info['id'] != $data['id']) exception('公司名称已存在');
        $result = $this->model->where('id',$data['id'])->update(['name'=>$data['name']]);
        if($result !== false){
            $this->result($result, '操作成功', 1);
        }
        $this->result($result, '操作失败', 0);
    }
    public function del(){
        $data = $this->request->param();
        $this->validate($data,[
            'id|操作id'=>'require',

        ]);
        $result = $this->model->where('id',$data['id'])->delete();
        if($result !== false){
            $this->result($result, '操作成功', 1);
        }
        $this->result($result, '操作失败', 0);
    }
}